<?php

class Post extends CActiveRecord {
    /**
     * The followings are the available columns in table 'z_post':
     * @var integer $id_post
     * @var string $title
     * @var string $description
     * @var string $text
     * @var integer $point
     * @var string $c_date
     * @var string $u_date
     * @var string $autor
     * @var integer $visible
     * @var string $tags
     */
    private $_oldTags;
    /**
     * Returns the static model of the specified AR class.
     * @return Post the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'z_post';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
                array('title, description, text', 'required'),
                array('point, visible', 'numerical', 'integerOnly'=>true),
                array('c_date, u_date, autor, tags', 'safe'),

                //array('tags', 'match', 'pattern'=>'/^[\w\s,]+$/', 'message'=>'Tags can only contain word characters.'),
                //array('tags', 'normalizeTags'),
                // The following rule is used by search().
                // Please remove those attributes that should not be searched.
                array('id_post, title, description, text, point, c_date, u_date, autor, visible, tags', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
                'id_post' => 'id',
                'title' => 'Заголовок',
                'description' => 'Описание',
                'text' => 'Текст',
                'point' => 'Оценка',
                'c_date' => 'Дата создания',
                'u_date' => 'Дата обновления',
                'autor' => 'Автор',
                'visible' => 'Видимость',
                'tags' => 'Тэги',
        );
    }


    protected function afterSave() {
        parent::afterSave();
        PostTag::model()->updateFrequency($this->_oldTags, $this->tags);
    }

    protected function afterFind() {
        parent::afterFind();
        $this->_oldTags=$this->tags;
    }
    protected function afterDelete() {
        parent::afterDelete();
        Coment::model()->deleteAll('id_post='.$this->id_post.' AND id_name = "post"');
        PostTag::model()->updateFrequency($this->tags, '');
    }
    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id_post',$this->id_post);

        $criteria->compare('title',$this->title,true);

        $criteria->compare('description',$this->description,true);

        $criteria->compare('text',$this->text,true);

        $criteria->compare('point',$this->point);

        $criteria->compare('c_date',$this->c_date,true);

        $criteria->compare('u_date',$this->u_date,true);

        $criteria->compare('autor',$this->autor,true);

        $criteria->compare('visible',$this->visible);

        $criteria->compare('tags',$this->tags,true);

        return new CActiveDataProvider('Post', array(
                        'criteria'=>$criteria,
        ));
    }
}